/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<string> binaryTreePaths(TreeNode *root)
    {
        vector<TreeNode *> path{root};
        vector<string> result;
        inorder(path, result);
        return result;
    }

    void inorder(vector<TreeNode *> &path, vector<string> &result)
    {
        TreeNode *node = path.back();
        if (node->left == nullptr && node->right == nullptr)
        {
            string pathStr = to_string(path[0]->val);
            for (int i = 1; i < path.size(); ++i)
            {
                pathStr += "->";
                pathStr += to_string(path[i]->val);
            }
            result.push_back(pathStr);
            return;
        }
        if (node->left != nullptr)
        {
            path.push_back(node->left);
            inorder(path, result);
            path.pop_back();
        }
        if (node->right != nullptr)
        {
            path.push_back(node->right);
            inorder(path, result);
            path.pop_back();
        }
    }
};